package com.sd.repay.transfer;

import com.sd.repay.dao.BaseDao;
import com.sd.repay.pojo.TransferChannelInfo;
import com.sd.repay.pojo.TransferOrder;
import com.sd.repay.pojo.TransferRoute;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 代付持久化操作类
 *
 * @author sd
 * @date 2018年9月5日17:07:04
 * @see 所有代付有关的持久化操作都将在这里描述
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class TransferDbService {

    @Autowired
    private BaseDao dao;


    /**
     * 初始化出款订单明细
     *
     * @param transferOrder 订单明细
     * @return 订单ID
     * @throws SQLException
     * @throws Exception
     */
    public Long saveTransferOrder(TransferOrder transferOrder) throws SQLException, Exception {
        List<Object> condition = new ArrayList<>();
        String sql = "INSERT INTO t_transfer_order(order_type,source_order_no,trade_source,mer_type,mer_no,out_pay_bank,acq_mer_no,out_acc_no,in_acc_no,in_token,in_acc_name,";
        sql += "in_settle_bank_no,in_bank_no,in_bank_name,mobile_no,identity_type,identity_no,amount,fee, STATUS,summary,bak1,bak2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        condition.add(transferOrder.getOrderType());
        condition.add(transferOrder.getSourceOrderNo());
        condition.add(transferOrder.getTradeSource());
        condition.add(transferOrder.getMerType());
        condition.add(transferOrder.getMerNo());
        condition.add(transferOrder.getOutPayBank());
        condition.add(transferOrder.getAcqMerNo());
        condition.add(transferOrder.getOutAccNo());
        condition.add(transferOrder.getInAccNo());
        condition.add(transferOrder.getInToken());
        condition.add(transferOrder.getInAccName());
        condition.add(transferOrder.getInSettleBankNo());
        condition.add(transferOrder.getInBankNo());
        condition.add(transferOrder.getInBankName());
        condition.add(transferOrder.getMobileNo());
        condition.add(transferOrder.getIdentityType());
        condition.add(transferOrder.getIdentityNo());
        condition.add(transferOrder.getAmount());
        condition.add(transferOrder.getFee());
        condition.add("0");
        condition.add(transferOrder.getSummary());
        condition.add(transferOrder.getBak1());
        condition.add(transferOrder.getBak2());

        return dao.getSaveObjId(sql, condition.toArray());
    }


    /**
     * 获取区间出款订单记录
     * <p>
     * <p>
     * 根据时间区间获取出款订单记录，status为订单状态，为空或Null则不计入查询条件之列
     *
     * @param startTime 起始时间
     * @param endTime   截止时间
     * @param status    出款状态
     * @return List<TransferOrder>
     */
    public List<TransferOrder> getTransferOrderListByTime(String startTime, String endTime, String... status) {
        String sql = "select * from t_transfer_order where create_time >= ? and create_time =< ?";
        if (status != null && status.length > 0) {
            sql += " and status in(";
            for (int i = 0; i <= status.length; i++) {
                sql += status[i];
                if (i != (status.length - 1)) {
                    sql += ",";
                }
            }
            sql += ")";
        }

        return dao.findList(TransferOrder.class, sql, startTime, endTime);
    }


    /**
     * 获取出款明细
     * <p>
     * <p>
     * 根据出款明细ID以及状态获取出款明细，订单状态为空或Null则不计入查询条件之列
     *
     * @param orderId 出款明细ID编号
     * @param status  出款状态
     * @return
     */
    public TransferOrder getTransferOrderById(String orderId, String status) {
        ArrayList<String> condition = new ArrayList<>();
        condition.add(orderId);
        String sql = "select * from t_transfer_order where id = ?";
        if (StringUtils.isNoneBlank(status)) {
            sql += " and status = ?";
            condition.add(status);
        }
        return dao.findFirst(TransferOrder.class, sql, condition.toArray());
    }


    /**
     * 获取出款明细
     * <p>
     * <p>
     * 根据订单类型、来源单号及出款状态获取出款明细，订单状态为空或Null则不计入查询条件之列
     *
     * @param orderType     订单类型
     * @param sourceOrderNo 来源订单
     * @param status        订单状态
     * @return TransferOrder
     */
    public TransferOrder getTransferOrder(String orderType, String sourceOrderNo, String status) {
        ArrayList<String> condition = new ArrayList<>();
        condition.add(orderType);
        condition.add(sourceOrderNo);
        String sql = "select * from t_transfer_order where order_type = ? and source_order_no = ?";
        if (StringUtils.isNoneBlank(status)) {
            sql += " and status = ?";
            condition.add(status);
        }
        return dao.findFirst(TransferOrder.class, sql, condition.toArray());
    }


    /**
     * 获取代付通道信息集合
     * <p>
     * <p>
     * 获取所有状态为正常代付通道信息
     *
     * @return List<TransferChannelInfo>
     */
    public List<TransferChannelInfo> getTransferChannelInfoList() {
        String sql = "select * from t_transfer_channel where bank_status = '1'  order by create_time desc";
        return dao.findList(TransferChannelInfo.class, sql);
    }


    /**
     * 获取代付通道信息
     * <p>
     * <p>
     * 根据代付通道编码获取状态为正常的代付通道信息
     *
     * @param payBankCode 代付通道编码
     * @return TransferChannelInfo
     */
    public TransferChannelInfo getTransferChannelInfo(String payBankCode) {
        String sql = "select * from t_transfer_channel where bank_code = ? and bank_status = '1'  order by create_time desc";
        return dao.findFirst(TransferChannelInfo.class, sql, payBankCode);
    }


    /**
     * 获取代付通道信息
     * <p>
     * <p>
     * 根据代付通道编码以及出款账户获取状态为正常的代付通道
     *
     * @param payBankCode 代付通道编码
     * @param outAccNo    出款账户
     * @return TransferChannelInfo
     */
    public TransferChannelInfo getTransferChannelInfo(String payBankCode, String outAccNo) {
        String sql = "select * from t_transfer_channel where bank_code = ? and bank_merchant_no = ? and bank_status = '1'  order by create_time desc";
        return dao.findFirst(TransferChannelInfo.class, sql, payBankCode, outAccNo);
    }


    /**
     * 获取出款路由
     * <p>
     * <p>
     * 根据交易来源获取状态为正常的代付路由信息
     *
     * @param tradeSource 交易来源
     * @return TransferRoute
     */
    public TransferRoute getTransferRoute(String tradeSource) {
        String sql = "select * from t_tranfers_route where trade_source = ? and status = '1'";
        return dao.findFirst(TransferRoute.class, sql, tradeSource);
    }
}
